package com.jxau.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxau.controller.utils.Result;
import com.jxau.domain.Movie;
import com.jxau.service.MovieService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/MoviesWebsite/movie")
public class MovieController {
    @Resource
    private MovieService movieService;

    //获取所有电影信息
    @GetMapping
    public Result getAll(){
        return new Result(true, movieService.list());
    }

    //添加电影
    @PostMapping
    public Result save(@RequestBody Movie movie){
        return new Result(movieService.save(movie));
    }

    //修改电影
    @PutMapping
    public Result modify(@RequestBody Movie movie){
        return new Result(movieService.updateById(movie));
    }

    //删除电影
    @DeleteMapping("{movieid}")
    public Result delete(@PathVariable Integer movieid){
        return new Result(movieService.removeById(movieid));
    }

    //按电影id查询电影
    @GetMapping("{movieid}")
    public Result getById(@PathVariable String movieid){
        Movie movie = movieService.getById(movieid);
        if(movie != null){
            return new Result(true, movie);
        }else{
            return new Result(false, null);
        }
    }

    //电影分页查询
    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, Movie movie){
        IPage<Movie> page = movieService.getPage(currentPage, pageSize, movie);
        if(currentPage > page.getPages()){
            page = movieService.getPage((int)page.getPages(), pageSize, movie);
        }
        return new Result(true, page);
    }


}
